{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04ca75de",
   "metadata": {},
   "source": [
    "# python基础入门5_ Lambda表达式&列表表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169e2b9c",
   "metadata": {},
   "source": [
    "Python中的Lambda表达式是一种简洁的创建匿名函数的方法。它允许你在一行代码中定义一个简单的函数，而不需要使用def关键字。Lambda表达式的基本语法如下：lambda 参数列表： 表达式\n",
    "\n",
    "#### Lambda表达式有以下几个优点：\n",
    "\n",
    "简洁：Lambda表达式可以用一行代码实现一个简单的函数，不需要使用def关键字和函数名。\n",
    "\n",
    "匿名：Lambda表达式没有函数名，因此不需要显式地将函数赋值给一个变量。这使得在需要一个小的、临时使用的函数时，可以更加方便地编写和传递。\n",
    "\n",
    "适用于高阶函数：Lambda表达式可以作为参数传递给其他函数，例如map()、filter()等高阶函数。这使得你可以使用简单的语法来处理复杂的操作。\n",
    "\n",
    "适用于并行计算：Lambda表达式可以很容易地用于并行计算，因为它们可以在多个线程或进程之间共享和执行。\n",
    "\n",
    "适用于数据预处理：在机器学习和数据分析中，经常需要对数据进行一些预处理操作，例如标准化、归一化等。Lambda表达式可以很方便地实现这些操作，而无需定义完整的函数。\n",
    "\n",
    "\n",
    "列表表达式是一种简洁的创建列表的方法，它使用方括号[]将一系列元素括起来，并用逗号,分隔。\n",
    "#### 列表推导式具有以下几个显著的优点：\n",
    "\n",
    "代码简洁：列表推导式能够在一行代码中完成生成列表的任务，相比于使用for循环来生成列表，代码量更少，更加简洁。这使得代码更易读、易理解，同时也减少了代码的维护工作。\n",
    "\n",
    "高效性能：列表推导式相比于for循环在性能上更为高效。由于列表推导式是内置函数，在底层进行了优化，可以以更快的速度完成列表的生成。\n",
    "\n",
    "功能更强大：列表推导式对于对列表进行变换和筛选非常方便和灵活。我们可以在表达式中加入判断语句，通过条件进行筛选，从而生成满足特定条件的列表。\n",
    "\n",
    "可读性更好：由于列表推导式的语法结构独特且简洁，代码更易读、易理解。在使用列表推导式时，无需在代码中额外说明循环的细节，使得代码更加清晰，提高了代码的可读性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "432a5fac",
   "metadata": {},
   "source": [
    "## lambda表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a6145b54",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add(x,y):\n",
    "    return x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "db19b928",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3d67645b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(x, y)>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##不需要显式地定义函数，直接传入匿名函数更方便。\n",
    "lambda x, y: x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9b11a692",
   "metadata": {},
   "outputs": [],
   "source": [
    "#匿名函数有个限制，就是只能有一个表达式，不用写return，返回值就是该表达式的结果。\n",
    "add = lambda x, y: x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e133385e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "17314d19",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_even(n):\n",
    "    if n%2==0:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e8a82a91",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用匿名函数有个好处，因为函数没有名字，不必担心函数名冲突。\n",
    "x = lambda x:x%2==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e168d441",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(x)>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "76f5da3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "dcdfc002",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0279d42f",
   "metadata": {},
   "source": [
    "## 列表表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "cabf7ef3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#自定义函数生成列表\n",
    "def crate_lst(n):\n",
    "    lst=[]\n",
    "    i=0\n",
    "    while i<n:\n",
    "        lst.append(i)\n",
    "        i+=1\n",
    "    return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "96011e76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=crate_lst(10)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "8bfad02c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#特殊函数生成列表\n",
    "lst = list(range(0,10))\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "b4aea4a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = list(range(0,10))\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "6e175ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表表达式生成列表\n",
    "lst = [i for i in range(0,10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "a069855f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "d72f98ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[i**2 for i in lst]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "203378f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "5043927c",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [i for i in lst if i%2==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "1fa5b610",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2acf9a62",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84212c37",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
